package com.cskaoyan.service.wx_catalog;

import com.cskaoyan.bean.wx_category.WxCategoryIndexVO;
import com.cskaoyan.bean.wx_category.WxCategoryVO;
import com.cskaoyan.bean.wx_goods.Category;
import com.cskaoyan.bean.wx_goods.CategoryExample;
import com.cskaoyan.mapper.wx_goods.WxCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: enhui
 **/
@Service
public class WxCatalogServiceImpl implements WxCatalogService {
    @Autowired
    WxCategoryMapper wxCategoryMapper;

    @Override
    public WxCategoryIndexVO catalogIndex() {
        WxCategoryIndexVO wxCategoryIndexVO = new WxCategoryIndexVO();
        //查询一级类目，按id排序
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("id");
        CategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
        categoryExampleCriteria.andLevelEqualTo("L1");
        List<Category> categoryList = wxCategoryMapper.selectByExample(categoryExample);
        wxCategoryIndexVO.setCategoryList(categoryList);
        WxCategoryVO wxCategoryVO = currentCatalog(categoryList.get(0).getId());
        wxCategoryIndexVO.setCurrentCategory(wxCategoryVO.getCurrentCategory());
        wxCategoryIndexVO.setCurrentSubCategory(wxCategoryVO.getCurrentSubCategory());
        return wxCategoryIndexVO;
    }

    @Override
    public WxCategoryVO currentCatalog(Integer id) {
        WxCategoryVO wxCategoryVO = new WxCategoryVO();
        //查询当前的一级类目，id最小的值
        Category currentCategory = wxCategoryMapper.selectByPrimaryKey(id);
        wxCategoryVO.setCurrentCategory(currentCategory);
        //查询当前的二级类目，根据pid查询
        CategoryExample categoryExampleForSub = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExampleForSub.createCriteria();
        criteria.andPidEqualTo(currentCategory.getId());
        List<Category> currentSubCategory = wxCategoryMapper.selectByExample(categoryExampleForSub);
        wxCategoryVO.setCurrentSubCategory(currentSubCategory);
        return wxCategoryVO;
    }
}
